package com.livgo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.livgo.dao.dto.UploadFileDto;
import com.livgo.dao.po.FileInfo;
import com.livgo.dao.po.PageBean;
import com.livgo.dao.po.Result;
import com.livgo.dao.vo.FileInfoVo;
import com.livgo.service.FileService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.MalformedURLException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

@RestController
@RequestMapping("/file")
@AllArgsConstructor
public class FileController {

    private final FileService fileService;

    @PostMapping("/upload")
    public Result<String> upload(@Valid @ModelAttribute UploadFileDto uploadFileDto) {
        String code = fileService.upload(uploadFileDto);
        return Result.success(code);
    }

    @GetMapping("/download/{id}")
    public ResponseEntity<byte[]> download(@PathVariable Integer id, HttpServletRequest request) {
        return fileService.download(id, request);
    }

    @GetMapping("/list")
    public Result<PageBean<FileInfoVo>> list(Integer pageNum, Integer pageSize) {
        PageBean<FileInfoVo> fvl = fileService.listPage(pageNum, pageSize);
        return Result.success(fvl);
    }

    @GetMapping("/info/{code}")
    public Result<FileInfoVo> info(@PathVariable String code) {
        FileInfoVo fiv = fileService.info(code);
        return Result.success(fiv);
    }

    @GetMapping("/detail/{id}")
    public Result<FileInfoVo> detail(@PathVariable Integer id) {
        FileInfo fi = fileService.getById(id);

        FileInfoVo fiv = BeanUtil.copyProperties(fi, FileInfoVo.class);

        return Result.success(fiv);
    }

    @DeleteMapping("/delete/{id}/{secretKey}")
    public Result delete(@PathVariable Integer id, @PathVariable String secretKey) {
        return fileService.delete(id, secretKey);
    }

}
